﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ArbolandiaBL;
using Ext.Net;

public partial class Admin_catalogConfigTips : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!ArbolandiaBL.Security.ValidLogin())
        {
            X.AddScript("this.parent.LogOut();");
        }
        if (!X.IsAjaxRequest)
        {
            dpfImageSizeCatHome.FieldLabel = ConstantsBL.SIZE_IMAGE_PRODUCT_CATEGORY_TITLE;
            string fileName = "tipsjardin.txt";
            string pathfile = Server.MapPath(@"~" + @"\" + "templates" + @"\" + fileName);
            if (File.Exists(pathfile)) { var content = File.ReadAllText(pathfile); htmlContent.Text = content; }
        }
    }
    protected void SaveButton_click(object sender, DirectEventArgs e)
    {
        try
        {
            string content = htmlContent.Text;
            string fileName = "tipsjardin.txt";
            string pathfile = Server.MapPath(@"~" + @"\" + "templates" + @"\" + fileName);
            if (File.Exists(pathfile)) { File.Delete(pathfile); }

            //TODO Remove
            LogError.WriteAppLog("FullPath : " + pathfile, MethodBase.GetCurrentMethod().Name);
            
            File.WriteAllText(pathfile, content);
        }
        catch (Exception ex)
        {
            
           ArbolandiaBL.LogError.WriteError(ex, System.Reflection.MethodBase.GetCurrentMethod().Name);
        }
        
    }
    protected void UploadClick(object sender, DirectEventArgs e)
    {

        try
        {
            string tpl = "Uploaded file: {0}<br/>Size: {1} bytes";
            if (this.uploadField.HasFile &&
                (this.uploadField.PostedFile.FileName.ToLower().Contains(".jpg"))
                )
            {

                string extension = Path.GetExtension(this.uploadField.PostedFile.FileName);
                string pathfile = "images\\tipsjardin";
                string filename = "tipsjardin" + ".jpg";
                pathfile = Server.MapPath(@"~" + @"\" + pathfile + @"\" + filename);

                //TODO Remove
                LogError.WriteAppLog("FullPath : " + pathfile, MethodBase.GetCurrentMethod().Name);

                new Service().SaveRezizeImage(pathfile, uploadField.PostedFile.InputStream, ConstantsBL.SIZE_IMAGE_PRODUCT_CATEGORY_HEIGHT, ConstantsBL.SIZE_IMAGE_PRODUCT_CATEGORY_WIDTH);

                //TODO Remove
                LogError.WriteAppLog("FullPath : " + pathfile, MethodBase.GetCurrentMethod().Name);

                imagenTip.ImageUrl = "../Images/tipsjardin/tipsjardin.jpg?" + DateTime.Now.Ticks;
                X.Msg.Show(new MessageBoxConfig
                {
                    Buttons = MessageBox.Button.OK,
                    Icon = MessageBox.Icon.INFO,
                    Title = "Exito",
                    Message = string.Format(tpl, this.uploadField.PostedFile.FileName, this.uploadField.PostedFile.ContentLength)
                });
                
            }
            else
            {
                X.Msg.Show(new MessageBoxConfig
                {
                    Buttons = MessageBox.Button.OK,
                    Icon = MessageBox.Icon.ERROR,
                    Title = "Error",
                    Message = "No se pudo cargar el archivo"
                });
            }
        }
        catch (Exception ex)
        {

            X.Msg.Show(new MessageBoxConfig
            {
                Buttons = MessageBox.Button.OK,
                Icon = MessageBox.Icon.ERROR,
                Title = "Error",
                Message = ex.Message
            });
            ArbolandiaBL.LogError.WriteError(ex, System.Reflection.MethodBase.GetCurrentMethod().Name);
        }
    }
}